package DGWeb.Servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DGWeb.AppConstants;
import DGWeb.Model.User;

/**
 * Servlet implementation class ViewMyProfile
 */
@WebServlet("/ViewMyProfile")
public class ViewMyProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewMyProfile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession UserSession = request.getSession();
		PrintWriter out = response.getWriter();
		InputStreamReader isr = new InputStreamReader(request.getInputStream());
		BufferedReader br = new BufferedReader(isr);
		StringBuilder jsonFileContent = new StringBuilder();
		//read line by line from file
		String nextLine = br.readLine();
		jsonFileContent.append(nextLine);
		
		Boolean ViewMemberProfile = jsonFileContent.toString().contains("Nickname"); // true if it has this sub string	
		
		if(ViewMemberProfile){
			String[] DataArray = jsonFileContent.toString().split(",");
			DataArray[0] = DataArray[0].substring(2); // Delete the {" from the begining of the string 
			String[] temp = DataArray[0].split(":");
			String Nickname = ( (String)temp[1].subSequence(1, temp[1].length()-2));
			UserSession.setAttribute("whichProfile",Nickname);
		}
	}
	

}
